<?php
namespace app\mouth\model;
use think\Model;
use think\Db;
class CircleIndexModel extends Model{
    public static function position($openid){
        return Db::name('circle_user')->field('id')->where('openid',$openid)->find();
    }
    public static function qufollow($main_id,$id){
        return Db::name('circle_follow')->where('main_id',$main_id)->where('u_id',$id)->delete();
    }
    public static function personalInformation($openid,$portrait,$gender,$nickname){
        $date['openid']     = $openid;
        $date['portrait']   = $portrait;
        $date['gender']     = $gender;
        $date['nickname']   = $nickname;
        $date['time']       = time();
        return Db::name('circle_user')->insertGetId($date);
    }
    public static function allContent($userid){
        $value['allCircles']    = Db::name('circle_invitation')->order('add_time desc')->select();
        $value['follow']        =Db::table('yc_circle_follow')
                                ->alias('f')
                                ->join('yc_circle_invitation i','f.main_id = i.u_id')
                                ->where('f.main_id',$userid['id'])
                                ->select();
        return $value;
    }

    public static function postComments($id){
        Db::name('circle_comment')->where('i_id',$id)->setInc('browse');
        return Db::name('circle_comment')->where('i_id',$id)->select();
    }


    public static function giveFabulous($p_id,$u_id,$i_id,$portrait,$nickname,$number){
        $surface = $number == 2 ? 'circle_like' : 'circle_fabulous';
        $user = Db::name($surface)->where('u_id',$u_id)->where('i_id',$i_id)->find();
        if ($user){
            return false;
        }
        $date['p_id']       = $p_id;
        $date['u_id']       = $u_id;
        $date['i_id']       = $i_id;
        $date['portrait']   = $portrait;
        $date['nickname']   = $nickname;
        $date['time']       = time();
        if ($number == 2){
            return Db::name('circle_like')->insert($date);
        }
        Db::name('circle_invitation')->where('id',$i_id)->setInc('fabulous');
        return Db::name('circle_fabulous')->insert($date);
    }
    public static function focusOnPeople($main_id,$u_id,$portrait,$nickname){
        $user = Db::name('circle_follow')->where('main_id',$main_id)->where('u_id',$u_id)->find();
        if ($user){
            return false;
        }
        $date['main_id']        = $main_id;
        $date['u_id']           = $u_id;
        $date['portrait']       = $portrait;
        $date['nickname']       = $nickname;
        $date['time']           = time();

//        $res = Db::name('circle_follow')->where('main_id',$u_id)->where('u_id',$main_id)->find();
//
//        if($res){
//            Db::name('circle_follow')->where('main_id',$u_id)->where('u_id',$main_id)->update(['trmy'=>2]);
//            $date['trmy'] = 2;
//        }
        return Db::name('circle_follow')->insert($date);
    }
    public static function myComments($value){
            if ($value['data'] == null){
                return '请输入值';
            }
            $date['i_id'] = $value['i_id'];
            $date['p_id'] = $value['p_id'];
            $date['u_id'] = $value['u_id'];
            $date['portrait'] = $value['portrait'];
//            $date['data'] = $value['data'];
            $date['nickname'] = $value['nickname'];
            $date['time'] = time();
        Db::name('circle_like')->insert($date);
        return true;
    }
}